package com.yesorno.activity;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yesorno.constants.Constants;
import com.yesorno.model.HttpResponseObject;
import com.yesorno.ui.CustomTitleActivity;
import com.yesorno.ui.MyMenuList;
import com.yesorno.utils.HttpUtils;

public class BlackListActivity extends CustomTitleActivity {

	private Button goBack;

	private ListView black_list_list;

	private long curUserId;

	RegBtnsClickListeners rbc = new RegBtnsClickListeners();

	/**
	 * 注册页面按钮的监听类
	 * 
	 * @author samoin
	 * @date 2011-07-06
	 * @version 0.1
	 * 
	 */
	class RegBtnsClickListeners implements OnClickListener {
		@Override
		public void onClick(View v) {
			if (v == goBack) {
				Intent intent = new Intent();
				BlackListActivity.this.setResult(RESULT_OK, intent);
				BlackListActivity.this.finish();
			}
			if (v.getId() == R.id.black_list_btn_remove) {
				// System.out.println(v);
			}
		}
	}

	@Override
	public void setTitleViews() {
		LinearLayout titleLayout = getTITLE_LAYOUT();
		// 返回按钮
		goBack = CustomTitleActivity.createTitleLeftButton(
				BlackListActivity.this, R.string.userinfo_title_btn_goback,
				Constants.TITLE_BTN_MARGIN1);
		goBack.setOnClickListener(rbc);
		titleLayout.addView((View) goBack.getParent());

		// 中间的资料的文字
		TextView tv = CustomTitleActivity.createTitleDesc(
				BlackListActivity.this, R.string.black_list_title, 0);
		titleLayout.addView((View) tv.getParent());

		TextView tv2 = CustomTitleActivity.createTitleDesc(
				BlackListActivity.this, 0, 0);
		titleLayout.addView((View) tv2.getParent());
	}

	@Override
	public void setLayoutId() {
		setContentView(R.layout.black_list);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		curUserId = Constants.USER_ID;
		super.onCreate(savedInstanceState);
		((LinearLayout) findViewById(R.id.menus)).addView(new MyMenuList(
				BlackListActivity.this, 2));

		setMyView();

		// TODO 获取该用户的黑名单
		HttpResponseObject hro = HttpUtils.getInfoByUrlPost(
				Constants.BLACK_LIST_URL + "userId=" + curUserId,
				BlackListActivity.this);
		try {
			JSONArray array = hro.getResult().getJSONArray("blacklist");
			ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
			for (int i = 0; i < array.length(); i++) {
				JSONObject jsonObject = array.getJSONObject(i);
				HashMap<String, Object> map = new HashMap<String, Object>();
				map.put("black_list_btn_logo", jsonObject.get("userLogo"));
				map.put("black_list_name", jsonObject.get("userName"));
				map.put("black_list_userid", jsonObject.get("userId"));
				list.add(map);
			}
			BlackListButtonAdapter adapter = new BlackListButtonAdapter(
					BlackListActivity.this,
					list,
					R.layout.black_list_list,
					new String[] { "black_list_btn_logo", "black_list_name",
							"black_list_btn_remove", "black_list_userid" },
					new int[] { R.id.black_list_btn_logo, R.id.black_list_name,
							R.id.black_list_btn_remove, R.id.black_list_userid });
			black_list_list.setAdapter(adapter);
		} catch (JSONException e) {
			e.printStackTrace();
		}

	}

	private void setMyView() {
		black_list_list = (ListView) findViewById(R.id.black_list_list);
		// 这句代码非常重要，用来设置点击的当前项的背景
		black_list_list.setSelector(R.drawable.list_bg);
		// 这句代码非常重要，用来设置拖动中的背景
		black_list_list.setCacheColorHint(0);
	}

	public class BlackListButtonAdapter extends BaseAdapter {
		private class buttonViewHolder {
			ImageView userLogo;
			TextView userName;
			TextView button;
			TextView userId;
		}

		private ArrayList<HashMap<String, Object>> mAppList;
		private LayoutInflater mInflater;
		private Context mContext;
		private String[] keyString;
		private int[] valueViewID;
		private buttonViewHolder holder;

		public BlackListButtonAdapter(Context c,
				ArrayList<HashMap<String, Object>> appList, int resource,
				String[] from, int[] to) {
			mAppList = appList;
			mContext = c;
			mInflater = (LayoutInflater) mContext
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			keyString = new String[from.length];
			valueViewID = new int[to.length];
			System.arraycopy(from, 0, keyString, 0, from.length);
			System.arraycopy(to, 0, valueViewID, 0, to.length);
		}

		@Override
		public int getCount() {
			return mAppList.size();
		}

		@Override
		public Object getItem(int position) {
			return mAppList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		public void removeItem(int position, String userId) {
			mAppList.remove(position);
			this.notifyDataSetChanged();
			// TODO 发消息删除这个黑名单
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView != null) {
				holder = (buttonViewHolder) convertView.getTag();
			} else {
				convertView = mInflater.inflate(R.layout.black_list_list, null);
				holder = new buttonViewHolder();
				holder.userLogo = (ImageView) convertView
						.findViewById(valueViewID[0]);
				holder.userName = (TextView) convertView
						.findViewById(valueViewID[1]);
				holder.button = (TextView) convertView
						.findViewById(valueViewID[2]);
				holder.userId = (TextView) convertView
						.findViewById(valueViewID[3]);
				convertView.setTag(holder);
			}

			HashMap<String, Object> appInfo = mAppList.get(position);
			if (appInfo != null) {
				String userLogo = (String) appInfo.get(keyString[0]);
				String userName = (String) appInfo.get(keyString[1]);
				String button = getResources().getString(
						R.string.black_list_btn_remove);
				String userId = appInfo.get(keyString[3]).toString();
				Handler handler = new Handler();
				CustomTitleActivity.BitmapThread r = new CustomTitleActivity.BitmapThread(
						userLogo, holder.userLogo, true);
				handler.post(r);
				holder.userName.setText(userName);
				holder.button.setText(button);
				holder.button.setOnClickListener(new BlackListButtonListener(
						position));
				holder.userId.setText(userId);
			}
			return convertView;
		}

		class BlackListButtonListener implements OnClickListener {
			private int position;

			BlackListButtonListener(int pos) {
				position = pos;
			}

			@Override
			public void onClick(View v) {
				int vid = v.getId();
				if (vid == holder.button.getId()) {
					TextView temp = (TextView) v;
					TextView userId = (TextView) ((View) temp.getParent())
							.findViewById(R.id.black_list_userid);
					removeItem(position, userId.getText().toString());
				}
			}
		}
	}

}
